<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page 
	import="java.security.*"
	import="java.io.*"
	import="java.net.*"
	import="java.util.*"%>
	<%@ page import="markan.*"%>
	<%@ page import="java.sql.*" %>   
	<%@page import="markan.fb.TCFacebookHelper"%> 
<%
	int facebookId = TCStringUtil.string2Integer(request.getParameter("id")); 
	String code = TCStringUtil.string2void(request.getParameter("code"));
	
	//토큰을 가져온다.
	Map<String, String> result = TCFacebookHelper.getAccessToken(code, facebookId);
			
	if( result != null ) {
		
		String accessToken = result.get("accessToken");
		String expires = result.get("expires");
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = new markan.db.TCConnection().getConnection();
			
			String sql= "UPDATE FACEBOOK_USER SET FACEBOOK_ACCESS_TOKEN = ?, FACEBOOK_ACCESS_EXPIRES = ?, LAST_UPDT_PNTTM = now() WHERE FACEBOOK_ID = ?";
			pstmt = conn.prepareStatement(sql); 
			pstmt.setString(1, accessToken);
			pstmt.setString(2, expires);
			pstmt.setInt(3, facebookId);
			pstmt.executeUpdate();
			
			if(conn.getAutoCommit()==false) {
				conn.commit();
			}
			
		} catch(SQLException e) {
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(pstmt!=null) pstmt.close();
			if(conn!=null) conn.close();
		}
	}
	
	response.sendRedirect("userDetail.jsp?id="+facebookId);
%>